package com.example.aestest;

import java.io.UnsupportedEncodingException;

import com.example.aestest2.R;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

public class MainActivity extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        HelloJNI jni = new HelloJNI();
        // Log.d("MainActivity", "jni.printHello():" + jni.printHello());;
        try
        {

            byte[] values = hexStringToByte("DEADBEEF0015010000010002010002000000000000000000E3");

            byte[] keys = hexStringToByte("0105181711151913041902111119030311050613161904181919060103050111");

            byte[] results = hexStringToByte("3C0E60FB90FDB3CE4E5D275AA04D89BB4E6612DE82C8A281C03DAD3FA3D8A5E6");

            
            Log.d("MainActivity", "jni.printHello():" + values.length + " " + keys.length);;
            for(int i=0;i<100;i++)
            {
                byte[] s = jni.aesEncodeByte(values, keys, values.length, keys.length);

                byte[] s11= jni.aesDecodeByte(results, keys, results.length, keys.length);
                
                Log.d("MainActivity", "jni.printHello() s:" + bytesToHexString(s,s.length));
                Log.d("MainActivity", "jni.printHello() s11:" + bytesToHexString(s11,s11.length));   
            }

        

        } catch (Exception e)
        {
            e.printStackTrace();
        }

        if (savedInstanceState == null)
        {
            getFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
        }
    }

    /**
     * convert HexString to byte[]
     * @param hex
     * @return
     */
    public static byte[] hexStringToByte(String hex)
    {
        int len = (hex.length() / 2);
        byte[] result = new byte[len];
        char[] achar = hex.toCharArray();
        for (int i = 0; i < len; i++)
        {
            int pos = i * 2;
            result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
        }
        return result;
    }

    /**
     * convert byte[] to HexString
     * @param bArray
     * @param length
     * @return
     */
    public static String bytesToHexString(byte[] bArray, int length)
    {
        StringBuffer sb = new StringBuffer(length);
        String sTemp;
        for (int i = 0; i < length; i++)
        {
            sTemp = Integer.toHexString(0xFF & bArray[i]);
            if (sTemp.length() < 2)
                sb.append(0);
            sb.append(sTemp.toUpperCase());
        }
        return sb.toString();
    }

    /**
     * convert char to byte
     * @do
     * @param c
     * @return
     */
    public static byte toByte(char c)
    {
        byte b = (byte) "0123456789ABCDEF".indexOf(c);
        return b;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings)
        {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment
    {

        public PlaceholderFragment()
        {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)
        {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }
    }

}
